IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetNumericDatabaseVersion]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetNumericDatabaseVersion]
GO

CREATE FUNCTION [dbo].[GetNumericDatabaseVersion]()
RETURNS int
AS
BEGIN
	declare @version VARCHAR(30)
	declare @numericVersion int

	select @version=VersionName
	from Version
	where Description = 'Insight'

	select @numericVersion = convert( int , 
	case when substring( @version , 1 , 1 ) LIKE '[0-9]' then substring( @version , 1 , 1 ) else '' end +
	case when substring( @version , 2 , 1 ) LIKE '[0-9]' then substring( @version , 2 , 1 ) else '' end +
	case when substring( @version , 3 , 1 ) LIKE '[0-9]' then substring( @version , 3 , 1 ) else '' end +
	case when substring( @version , 4 , 1 ) LIKE '[0-9]' then substring( @version , 4 , 1 ) else '' end +
	case when substring( @version , 5 , 1 ) LIKE '[0-9]' then substring( @version , 5 , 1 ) else '' end +
	case when substring( @version , 6 , 1 ) LIKE '[0-9]' then substring( @version , 6 , 1 ) else '' end +
	case when substring( @version , 7 , 1 ) LIKE '[0-9]' then substring( @version , 7 , 1 ) else '' end +
	case when substring( @version , 8 , 1 ) LIKE '[0-9]' then substring( @version , 8 , 1 ) else '' end +
	case when substring( @version , 9 , 1 ) LIKE '[0-9]' then substring( @version , 9 , 1 ) else '' end
	)

	return  @numericVersion
END
GO
